package Janelas.SQL;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Banco.ConnectionFactory;

public class CadastroUser extends JFrame {

	private static final long serialVersionUID = 2L;
	private JPanel contentPane;
	private JTextField nomeUs;
	private JTextField CPFUs;
	private JTextField senhaUs;
	private JButton button;

	public CadastroUser() {
		setTitle("Cadastro - Occurence Safe");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 550, 260);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel(
				"Para se cadastrar no programa, preencha os campos abaixo:");
		label.setFont(new Font("Tahoma", Font.PLAIN, 14));
		label.setBounds(80, 11, 373, 17);
		contentPane.add(label);

		JLabel label_1 = new JLabel("Nome:");
		label_1.setBounds(110, 68, 70, 14);
		contentPane.add(label_1);

		nomeUs = new JTextField();
		nomeUs.setToolTipText("Somente o nome e o sobrenome");
		nomeUs.setColumns(10);
		nomeUs.setBounds(159, 65, 132, 20);
		contentPane.add(nomeUs);

		JLabel label_2 = new JLabel("CPF:");
		label_2.setBounds(121, 94, 62, 14);
		contentPane.add(label_2);

		CPFUs = new JTextField();
		CPFUs.setToolTipText("Insira somente n\u00FAmeros");
		CPFUs.setColumns(10);
		CPFUs.setBounds(159, 91, 132, 20);
		contentPane.add(CPFUs);

		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(110, 119, 70, 14);
		contentPane.add(lblSenha);

		senhaUs = new JTextField();
		senhaUs.setToolTipText("Confirme o seu CPF");
		senhaUs.setColumns(10);
		senhaUs.setBounds(159, 116, 132, 20);
		contentPane.add(senhaUs);

		button = new JButton("Cadastrar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Connection con;
				String query;

				query = "INSERT INTO USUARIO (NOME_USUARIO, CPF_USUARIO, SENHA_USUARIO) VALUES (?,?,?);";
				try {
					con = ConnectionFactory.getConnection();
					PreparedStatement stmt = con.prepareStatement(query);
					stmt.setString(1, nomeUs.getText());
					stmt.setString(2, CPFUs.getText());
					stmt.setString(3, senhaUs.getText());

					stmt.executeUpdate();
					stmt.close();
					con.close();
				} catch (SQLException e1) {

					e1.printStackTrace();
				}
				nomeUs.getText();
				CPFUs.getText();
				senhaUs.getText();

				dispose();
			}
		});
		button.setBounds(394, 90, 102, 23);
		contentPane.add(button);

		JLabel label_4 = new JLabel("Occurence Safe");
		label_4.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 20));
		label_4.setBounds(10, 188, 144, 24);
		contentPane.add(label_4);
	}

}
